﻿using GDGeek;
using MrPP.Myth;
using System;
using UnityEngine;

namespace MrPP.SerriedCabinet
{
    public class Follower : MonoBehaviour
    {
        [SerializeField]
        private FollowerModel _model;
        private bool controler_ = false;

        void Start() {
            _model.onPoseChange += doPoseChange;
        }

        private void doPoseChange(Yggdrasil.AsgardPose arg1, Yggdrasil.AsgardPose pose)
        {
            if (!controler) { 
                Yggdrasil.WorldPose world = Yggdrasil.Instance.getWorldPose(pose);
                if (Vector3.Distance(world.position, transform.position) < 0.3f)
                {
                    TweenTransformData.Begin(gameObject, 0.1f,new TransformData(world.position, Quaternion.LookRotation(world.forward, world.up), world.scale));
                }
                else
                {
                    transform.position = world.position;
                    transform.rotation = Quaternion.LookRotation(world.forward, world.up);
                    transform.setGlobalScale(world.scale);
                }
            }
        }

        void OnDestroy() {

            if (_model) {
                _model.onPoseChange -= doPoseChange;
            }
        }
        public bool controler {
            set;
            private get;
        }

        void Update() {
            if (controler) {
                if (this.transform.hasChanged) {
                    MrPP.Myth.Yggdrasil.AsgardPose pose = MrPP.Myth.Yggdrasil.Instance.GetAsgardPose(this.transform);
                    Bridge.Instance.post(_model.handle, "transform", pose);

                }

}
        }
    }
}
